<%@ page contentType="text/html;charset=UTF-8" language="java" %> 
<%@ page import="java.util.List" %> 
<%@ page import="javax.jdo.PersistenceManager" %> 
<%@ page import="com.google.appengine.api.users.User" %> 
<%@ page import="com.google.appengine.api.users.UserService" %> 
<%@ page import="com.google.appengine.api.users.UserServiceFactory" %> 
<%@ page import="orm.Stock" %> 
<%@ page import="orm.PMF" %> 
<%@ page import="javax.jdo.Query"%>
<%@ page import="pojo.StockDailyInfo"%>
<%@ page import="util.CrawlUtil"%>
<%@ page import="java.util.Date"%>
<%@ page import="util.DateUtil"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<style type="text/css">
@import "/css/jquery.datepick.css";
</style>
<link rel="stylesheet" type="text/css" href="/css/main.css" />
<link rel="stylesheet" type="text/css" href="/css/jquery.autocomplete.css" />

<script type="text/javascript" src="/js/jquery.js"></script>
<script type='text/javascript' src='/js/jquery.autocomplete.min.js'></script>
<script type='text/javascript' src='/js/stocks.js' charset="GBK"></script>
<script type="text/javascript" src="/js/jquery.datepick.min.js"></script>
<script type="text/javascript" src="/js/jquery.datepick-zh-CN.js"></script>

<script type="text/javascript">
$().ready(function() {

	function findValueCallback(event, data, formatted) {
		document.getElementById("selectedStock").value = data;
		//$("<li>").html( !data ? "No match!" : "Selected: " + formatted).appendTo("#result");
	}
	

	$("#stock").autocomplete(stocks, {
		matchContains: true,
		minChars: 0
	});


	$(":text, textarea").result(findValueCallback).next().click(function() {
		$(this).prev().search();
	});

	$('#startDate').datepick($.extend({showStatus: true, 
	  showOn: 'both', buttonImageOnly: true, buttonImage: '/img/calendar.gif', 
	  altField: '#90nAlternate', altFormat: 'yy-mm-dd'}));

	$('#endDate').datepick($.extend({showStatus: true, 
	  showOn: 'both', buttonImageOnly: true, buttonImage: '/img/calendar.gif', 
	  altField: '#90nAlternate', altFormat: 'yy-mm-dd'}));

});

function checkForm(theForm) {
	if (document.getElementById('selectedStock').value == '') {
		alert('请选择股票');
		return false;
	}
	if (document.getElementById('startDate').value == '' || document.getElementById('endDate').value == '') {
		alert('请选择监控时间段');
		return false;
	}

	return true;
}
</script>   
</head>
 <body>

<div id="content">
	<form autocomplete="off" action="/stock" method="post" onsubmit="return checkForm(this);">
	<p>
		<label>选择要监控的股票:</label>
		<input type="text" id="stock" />
		<input type="hidden" id="selectedStock" name="selectedStock" />
		<br/>
		<label>选择监控的时间段:</label>
		<input type="text" id="startDate" name="startDate" readonly="true"/> - <input type="text" id="endDate" name="endDate" readonly="true"/>
		<br/>
		<input type="submit" value="监控" />
	</p>
	</form>

<% 
    PersistenceManager pm = PMF.get().getPersistenceManager(); 
	Query query = pm.newQuery(Stock.class);
	query.setOrdering("gmtCreate desc");
	query.setRange(0, 1000);
    List<Stock> stocks = (List<Stock>) query.execute(); 
    if (stocks.isEmpty()) { 
%> 
<p>没有股票信息.</p> 
<% 
    } else { 
        for (Stock s : stocks) { 
%> 
<p><b><%= s.getName() %>(<%= s.getCode() %>)</b> 从<%= DateUtil.formatDate(s.getStartDate(), "yyyy-MM-dd") %>监控到<%= DateUtil.formatDate(s.getEndDate(), "yyyy-MM-dd") %> <a href="/stock?action=delete&key=<%= s.getId() %>">删除</a></p>
<p>开盘价:<%= s.getOpenPrice() %>  收盘价:<%= s.getEndPrice() %>  期间涨幅:<%= (float) Math.round( (s.getEndPrice() - s.getOpenPrice()) /s.getOpenPrice() * 100 * 100 ) / 100 %>%</p>
<p>最高价:<%= s.getHighPrice() %>  最低价:<%= s.getLowPrice() %>  最大差幅:<%= (float) Math.round( (s.getHighPrice() - s.getLowPrice()) /s.getLowPrice() * 100 * 100 ) / 100 %>%</p>
<%
List<StockDailyInfo> infos = (List<StockDailyInfo>) CrawlUtil.CrawlStockPriceInfo(s); 
if (!infos.isEmpty()) {
%>
<table width="100%">
	<tr><td>日期</td><td>开盘价</td><td>最高价</td><td>最低价</td><td>收盘价</td><td>成交量</td></tr>
<%
	for (StockDailyInfo si : infos) {
%>
	<tr><td><%= DateUtil.formatDate(si.getDate(), "yyyy-MM-dd") %></td><td><%= si.getOpenPrice() %></td><td><%= si.getHighPrice() %></td><td><%= si.getLowPrice() %></td><td><%= si.getClosePrice() %></td><td><%= si.getVolume() %></td></tr>
<%
	}
%>
</table>
<%
}
%>
<!--
<script type="text/javascript" src="http://hq.sinajs.cn/list=<%= s.getCode() %>" charset="gb2312"></script>
<script type="text/javascript">
var elements=hq_str_<%= s.getCode() %>.split(",");
document.write("今日开盘价:" + elements[1] + "<br/>");
document.write("昨日收盘价:" + elements[2] + "<br/>");
document.write("当前价格:" + elements[3] + "<br/>");
document.write("今日最高价:" + elements[4] + "<br/>");
document.write("今日最低价:" + elements[5] + "<br/>");
document.write("成交量:" + elements[8] + "<br/>");
document.write("成交金额:" + elements[9]/10000 + "万元");
</script>
 -->
 <!-- 
<img src="http://gubit.cn/SuperView/<%= DateUtil.formatDate(new Date(), "yyMMdd") %>/<%= s.getCode() %>.jpg"/>
 -->
<% 
		} 
    } 
    pm.close(); 
%> 	

</div> 
  </body> 
</html>